import {message, Button, Divider, DatePicker} from 'antd'
import React, { useState, useRef} from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect, Link } from 'umi'
import { queryDimissionAuditList, saveDimissionAudit, dimissionAuditBpm, updateDimissionAudit } from './service'
import CreateForm from './components/CreateForm'
import moment from 'moment';
import { modeList, dimissionTypeList,  BPMStatusEnum } from '@/utils/constant'

const { RangePicker } = DatePicker;

const TableList = (props) => {
  const actionRef = useRef();
  const [done, setDone] = useState(false);
  const [visible, setVisible] = useState(false);
  const [current, setCurrent] = useState(undefined);
  const columns = [
    {
      title: '工号',
      dataIndex: 'code',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
      width:60,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
      hideInSearch: true,
    },
    {
      title: '职级',
      dataIndex: 'jobLevel',
      hideInSearch: true,
    },
    {
      title: '公司名称',
      dataIndex: 'orgname',
      hideInSearch: true,
    },
    {
      title: '部门名称',
      dataIndex: 'unitname',
      hideInSearch: true,
    },
    /*{
      title: '体系',
      dataIndex: 'jobSystem',
    },
    {
      title: '条线',
      dataIndex: 'line',
      hideInSearch: true,
    },*/
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      renderFormItem: (item, props) => {
        return <RangePicker {...props} />;
      },
    },
    {
      title: '司龄',
      dataIndex: 'workingYears',
      hideInSearch: true,
    },
    {
      title: '审批状态',
      dataIndex: 'status',
      hideInSearch: true,
      valueEnum: BPMStatusEnum
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          {
            record.status===0 ?
              <>
                <a onClick={() => Initiate(record)}>发起</a>
                <Divider type="vertical"/>
                <a onClick={() => edit(record)}>编辑</a>
              </> : ''
          }
          {(record.bpmDetailUrl && record.status!==0) && <><a onClick={() => window.open(record.bpmDetailUrl)}>审批详情</a></>}
        </>
      )
    }
  ]

  const handleCancel = () => {
    setCurrent(null);
    setVisible(false)
  };

  const addResign = () => {
    setVisible(true);
    setCurrent(null);
  };

  const edit = (record) => {
    setVisible(true);
    setCurrent(record);
  };

  const Initiate = (record) => {
    dimissionAuditBpm(record).then(res => {
      if (res.flag){
        window.open(res.data);
        message.success('发起成功！');
      }
    })
  };

  const handleSubmit = (values) => {
    if (current){
      updateDimissionAudit(values).then(res => {
        if (res.flag){
          message.success('离职审计修改成功！');
        }else {
          message.error(res.error);
        }
        setDone(false);
        setCurrent(null);
        setVisible(false);
        actionRef.current.reload()
      });
    }else {
      saveDimissionAudit(values).then((r)=>{
        if (r.flag){
          message.success('离职审计发起成功！');
        }else {
          message.error(r.error);
        }
        setDone(false);
        setCurrent(null);
        setVisible(false);
        actionRef.current.reload()
      })
    }
  };

  return (
    <PageHeaderWrapper>
          <ProTable
            actionRef={actionRef}
            rowKey="id"
            request={(params) => queryDimissionAuditList(params)}
            columns={columns}
            toolBarRender={(action, { selectedRows }) => [
              <Button type="primary" onClick={() => addResign(null)}>新建</Button>,
              // <Button type="primary" onClick={() => delResign()}>删除</Button>,
            ]}
          />
          <CreateForm
            done={done}
            current={current}
            visible={visible}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
    </PageHeaderWrapper>
  )
}

export default connect(({  }) => ({
}))(TableList)
